import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Solution1122 {
    public int[] relativeSortArray(int[] arr1, int[] arr2) {
        Map<Integer,Integer> map=new HashMap<>();
        for (int i : arr2) {
            map.put(i,0);
        }
        for (int i : arr1) {
            if (map.containsKey(i)){
                map.put(i,map.get(i)+1);
            }
        }
        int c=0;
        int[] ans=new int[arr1.length];
        for (int i : arr2) {
            for (int j=0;j<map.get(i);j++){
                ans[c]=i;
                c++;
            }
        }
        int r=c;
        for (int i : arr1) {
            if (!map.containsKey(i)){
                ans[c]=i;
                c++;
            }
        }
        Arrays.sort(ans,r,c);
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(new Solution1122().relativeSortArray(new int[]{2,3,1,3,2,4,6,7,9,2,19},new int[]{2,1,4,3,9,6})));
    }
}
